﻿// var address = require("../../../mock.js")
var app = getApp();
var config = require('./config.js');
var depth = 1;
var provinces= [];
var citys={};
var areas= {};
Page({
    onLoad: function (options) {
       this.getData();
    },
   //获取一二级单位
   getData: function () {
    var that = this;
    wx.request({
      url: config.getExamUnit,
      method: "POST",
      data: {
        examId: '70'
      },
      success: function (ret) {
        var data = ret.data
        if (data.success == 0) {
          var list = data.rows;
         that.handler(data.rows,depth);
        }
      }
    })
  },

    handler:function(data, depth, parent_id, parent_name) {
        if (Array.isArray(data)) {
          data.forEach(({id, unitName, examUnits }) => {
            switch (true) {
              case depth === 1:
                provinces.push({ id, name: unitName });
                console.log(provinces)
                break;
              case depth === 2:
                if (!citys[parent_id]) citys[parent_id] = [];
                citys[parent_id].push({ id, name: unitName, province: parent_name });
                console.log(citys)
                break;
              case depth === 3:
                if (!areas[parent_id]) areas[parent_id] = [];
                areas[parent_id].push({ id, name: unitName, province: parent_name });
                console.log(areas)
                break;
              default:
                break;
            }
            if (Array.isArray(examUnits))
            {
             this.handler(examUnits, depth + 1, id, unitName);
            } 
          });
        }
      }
})